package edu.bu.grocerylist.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import edu.bu.grocerylist.model.GroceryUser;

/**
 * Basic DAO for Grocery User objects
 * @author donlanp
 *
 */
public class GroceryUserDao extends SqlSessionDaoSupport implements IGroceryUserDao {

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryUserDao#getAllUsers()
	 */
	public List<GroceryUser> getAllUsers(){
		return getSqlSession().selectList("selectUsers");
	}

	/*
	 * (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryUserDao#getUserByCredentials(java.lang.String, java.lang.String)
	 */
	public GroceryUser getUserByCredentials(String username, String password) {
		GroceryUser query = new GroceryUser();
		query.setEmail(username);
		query.setPassword(password);
		return getSqlSession().selectOne("selectUserByCredentials",query);
	}

	/*
	 * (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryUserDao#getUsersByUsername(java.lang.String)
	 */
	public List<GroceryUser> getUsersByUsername(String username) {
		return getSqlSession().selectList("selectUsersByUsername",username);
	}

	/*
	 * (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryUserDao#getUserByUserId(int)
	 */
	public GroceryUser getUserByUserId(int userId) {
		return getSqlSession().selectOne("selectUserByUserid",userId);
	}

	/*
	 * (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryUserDao#saveUser(edu.bu.grocerylist.model.GroceryUser)
	 */
	public int saveUser(GroceryUser newUser) {
		System.out.println(newUser.getSecurityQuestion() + " :: " + newUser.getSecurityAnswer());
		return getSqlSession().insert("insertUser",newUser);
	}

	/*
	 * (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryUserDao#updateUser(edu.bu.grocerylist.model.GroceryUser)
	 */
	public int updateUser(GroceryUser userToUpdate) {
		
		return getSqlSession().update("updateUser",userToUpdate);
	}

	/*
	 * (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryUserDao#getUserByUserId(String)
	 */
	public GroceryUser getUserByUserId(String username) {
		
		return getSqlSession().selectOne("selectUsersByUsername", username);
	}

	public void deleteUser(GroceryUser user) {
		getSqlSession().delete("deleteUser", user);
	}
	
	
	
}
